package com.example.sunmiv1.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.PriceQueryBean;
import com.example.sunmiv1.views.MyGridview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PriceSkuClassAdapter extends BaseAdapter {

    private Context context;
    private List<PriceQueryBean.GoodslistBean.SkuinfoBean> list;
    ViewHolder viewHolder;
    private List<PriceQueryBean.GoodslistBean.SkuinfoBean.InfoBean> infolist;
    private SkuBean skuBean;

    public PriceSkuClassAdapter(Context context, List<PriceQueryBean.GoodslistBean.SkuinfoBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.price_sku_class_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.skuClassAdapterTitle.setText(list.get(position).getName());
        infolist = new ArrayList<>();
        for (int i = 0; i < list.get(position).getInfo().size(); i++) {
            infolist.add(list.get(position).getInfo().get(i));
        }
        final PriceSkuInfoAdapter infoAdapter = new PriceSkuInfoAdapter(context, infolist);
        viewHolder.skuClassAdapterGridView.setAdapter(infoAdapter);
        infoAdapter.notifyDataSetChanged();
        infoAdapter.setSelect(9999);
        final int index = position;
        viewHolder.skuClassAdapterGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                infoAdapter.setSelect(position);
                skuBean.success(index, position, list.get(index).getInfo().get(position).getPath());
            }
        });
        return convertView;
    }

    public void Sku(SkuBean skuBean) {
        this.skuBean = skuBean;
    }

    public interface SkuBean {
        void success(int index, int position, String path);
    }

    static class ViewHolder {
        @BindView(R.id.skuclass_adapter_title)
        TextView skuClassAdapterTitle;
        @BindView(R.id.skuclass_adapter_gridview)
        MyGridview skuClassAdapterGridView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
